package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.WaterDailyUsage;
import com.ruoyi.system.domain.WaterMeterDatahistory;
import com.ruoyi.system.mapper.WaterDailyUsageMapper;
import com.ruoyi.system.mapper.WaterMeterDatahistoryMapper;
import com.ruoyi.system.service.IWaterDataService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description todo
 * @data 2025/9/25 11:19
 */
public class WaterDataServiceImpl implements IWaterDataService {

    @Autowired
    private WaterMeterDatahistoryMapper waterMeterDatahistoryMapper;

    @Autowired
    private WaterDailyUsageMapper waterDailyUsageMapper;

    // 按表号和时间排序抄表记录
    private Map<String, List<WaterMeterDatahistory>> getSortedReadingsByMeter() {
        WaterMeterDatahistory waterMeterDatahistory = new WaterMeterDatahistory();
        waterMeterDatahistory.setMeterType("0"); //远传表
        List<WaterMeterDatahistory> list = waterMeterDatahistoryMapper.selectWaterMeterDatahistoryList(waterMeterDatahistory);
        return list.stream()
                .sorted(Comparator.comparing(WaterMeterDatahistory::getReadDate))
                .collect(Collectors.groupingBy(WaterMeterDatahistory::getMeterNo));
    }

    /**
     * 计算每日用水量
     */
    @Override
    public void calculateDailyUsage(String meterNo) {
        List<WaterDailyUsage> dailyUsageList = new ArrayList<>();
        List<WaterMeterDatahistory> list = getSortedReadingsByMeter().get(meterNo);
        if (list == null || list.size() < 2) {
            return;
        }
        // 过滤指定日期范围内的记录
//        List<WaterMeterDatahistory> filteredReadings = list.stream()
//                .filter(r -> !r.getReadDate().before(startDate) && !r.getReadingDate().after(endDate))
//                .collect(Collectors.toList());

        for (int i = 1; i < list.size(); i++) {
            WaterMeterDatahistory prev = list.get(i - 1);
            WaterMeterDatahistory curr = list.get(i);
            BigDecimal p = new BigDecimal(prev.getCurrentReading());
            BigDecimal c = new BigDecimal(curr.getCurrentReading());
            BigDecimal dailyUsage = c.subtract(p);
            if (dailyUsage.compareTo(BigDecimal.ZERO) > 0){
                WaterDailyUsage waterDailyUsage = new WaterDailyUsage();
                waterDailyUsage.setMeterNo(meterNo);
                waterDailyUsage.setUserId(list.get(i).getUserId());
                waterDailyUsage.setUserName(list.get(i).getUserName());
                waterDailyUsage.setCurrentReading(c);
                waterDailyUsage.setPreviousReading(p);
                waterDailyUsage.setDailyConsumption(dailyUsage);
                waterDailyUsage.setReadingDate(curr.getReadDate());
                dailyUsageList.add(waterDailyUsage);
            }
        }
        waterDailyUsageMapper.insertWaterDailyUsageList(dailyUsageList);
    }

    @Override
    public void calculateWeeklyUsage() {

    }

    @Override
    public void calculateMonthlyUsage() {

    }
}
